﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Entity;
using System.Data.SqlClient;
using System.Data;

namespace Business.Dal
{
    public class IlceProvider : Provider<Ilceler>
    {
        public override bool Insert(Ilceler tObject)
        {
            bool returnValue = false;

            SqlCommand cmd = new SqlCommand("IlceEkle");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.Parameters.Add("SehirID", SqlDbType.Int).Value = tObject.SehirID;
            cmd.Parameters.Add("IlceAdi", SqlDbType.NVarChar).Value = tObject.IlceAdi;

            tObject.IlceID = Convert.ToInt32(cmd.ExecuteScalar());
            returnValue = true;
            return returnValue;




        }

        public override bool Update(Ilceler tObject)
        {
            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("IlceGuncelle");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.Parameters.Add("@IlceID", SqlDbType.Int).Value = tObject.IlceID;
            cmd.Parameters.Add("@SehirID", SqlDbType.Int).Value = tObject.SehirID;
            cmd.Parameters.Add("@IlceAdi", SqlDbType.NVarChar).Value = tObject.IlceAdi;

            returnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());
            return returnValue;


        }

        public override bool Delete(int RecordID)
        {
            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("IlceSil");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();

            cmd.Parameters.Add("@IlceID", SqlDbType.Int).Value = RecordID;

            returnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());

            return returnValue;
        }

        public List<Ilceler> GetAllRows()
        {
            List<Ilceler> returnValue = new List<Ilceler>();
            SqlCommand cmd = new SqlCommand("IlcelerGetir");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                Ilceler I = new Ilceler();
                I.IlceID = dr["IlceID"] is DBNull ? 0 : Convert.ToInt32(dr["IlceID"]);
                I.SehirID = dr["SehirID"] is DBNull ? 0 : Convert.ToInt32(dr["SehirID"]);
                I.IlceAdi = dr["IlceAdi"] is DBNull ? String.Empty : dr["IlceAdi"].ToString();

                returnValue.Add(I);
            }
            dr.Dispose();
            return returnValue;

        }

        public Ilceler IlceGetirIlceID(int RecordID)
        {
            SqlCommand cmd = new SqlCommand("IlceGetirIlceID");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@IlceID", RecordID);
            cmd.Connection = Tools.Connection();
            SqlDataReader dr;
            Tools.Open();
            dr = cmd.ExecuteReader();
            
            dr.Read();
            Ilceler ilce = new Ilceler();
            ilce.IlceID = dr["IlceID"] is DBNull ? 0 : Convert.ToInt32(dr["IlceID"]);
            ilce.SehirID = dr["SehirID"] is DBNull ? 0 : Convert.ToInt32(dr["SehirID"]);
            ilce.IlceAdi = dr["IlceAdi"] is DBNull ? String.Empty : dr["IlceAdi"].ToString();


            dr.Dispose();

            return ilce;

        }

     

        public List<Ilceler> IlceGetir(int SehirID)
        {
            List<Ilceler> returnValue = new List<Ilceler>();
            SqlCommand cmd = new SqlCommand("IlceGetir");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@SehirID", SqlDbType.Int).Value = SehirID;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                Ilceler I = new Ilceler();
                I.IlceID = dr["IlceID"] is DBNull ? 0 : Convert.ToInt32(dr["IlceID"]);
                I.SehirID = dr["SehirID"] is DBNull ? 0 : Convert.ToInt32(dr["SehirID"]);
                I.IlceAdi = dr["IlceAdi"] is DBNull ? String.Empty : dr["IlceAdi"].ToString();

                returnValue.Add(I);
            }
            dr.Dispose();
            return returnValue;

        }
    }
}
